package com.ews.onlineexchange.controller.activitii;

import java.util.List;

import javax.inject.Inject;

import org.activiti.engine.TaskService;
import org.activiti.engine.task.Task;
import org.activiti.engine.task.TaskQuery;
import org.apache.commons.lang3.StringUtils;

import com.ews.onlineexchange.service.EntityQuery;
import com.ews.onlineexchange.service.UserService;

public abstract class BpmList {
	
	@Inject
	protected TaskService			taskService;

	@Inject
	protected UserService			userService;



	protected String				processKey;

	protected Integer				firstResult	= 0;
	protected Integer				maxResults	= EntityQuery.MAX_RESULTS_NORMAL;

	Long							resultCount;
	List<Task>						resultList;
	
	public List<Task> getResultList() {
		if ( this.resultList == null ) {
			initResultList();
		}
		return this.resultList;
	}

	private void initResultList() {
		TaskQuery taskQuery = createTaskQuery();
		if ( StringUtils.isNotBlank( getProcessKey() ) ) {
			taskQuery.processDefinitionKey( getProcessKey() );
		}
		List<Task> list = null;
		if ( getMaxResults() == null ) {
			list = taskQuery.list();
		} else {
			list = taskQuery.listPage( getFirstResult(), getMaxResults() );			
		}
		this.resultCount = taskQuery.count();		
		
		this.resultList = list;
	}

	abstract TaskQuery createTaskQuery();

	public String getProcessKey() {
		return processKey;
	}

	public void setProcessKey( String processKey ) {
		this.processKey = processKey;
	}

	public Integer getFirstResult() {
		return firstResult;
	}

	public void setFirstResult( Integer firstResult ) {
		this.firstResult = firstResult;
	}

	public Integer getMaxResults() {
		return maxResults;
	}

	public void setMaxResults( Integer maxResults ) {
		this.maxResults = maxResults;
	}
	
	public Long getResultCount() {
		return resultCount;
	}

	public void setResultCount( Long resultCount ) {
		this.resultCount = resultCount;
	}

	public boolean isNextExists() {
		getResultList();
		return resultList!=null && getMaxResults()!=null &&
	             resultList.size() > getMaxResults();
	}
	
	public boolean isPreviousExists() {
		return getFirstResult() != null && getFirstResult() != 0;
	}
	
	public boolean isPaginated() {
		return isNextExists() || isPreviousExists();
	}

	public Long getLastFirstResult() {
		Integer pc = getPageCount();
		return pc == null ? null : (pc.longValue() - 1) * getMaxResults();
	}

	/**
	 * Get the index of the first result of the next page
	 * 
	 */
	public int getNextFirstResult() {
		Integer fr = getFirstResult();
		return (fr == null ? 0 : fr) + getMaxResults();
	}

	/**
	 * Get the index of the first result of the previous page
	 * 
	 */
	public int getPreviousFirstResult() {
		Integer fr = getFirstResult();
		Integer mr = getMaxResults();
		return mr >= (fr == null ? 0 : fr) ? 0 : fr - mr;
	}
   
	/**
	 * Get the total number of pages
	 * 
	 */
	public Integer getPageCount() {
		if (getMaxResults() == null) {
			return null;
		} else {
			int rc = getResultCount().intValue();
			int mr = getMaxResults().intValue();
			int pages = rc / mr;
			return rc % mr == 0 ? pages : pages + 1;
		}
	}

	public static String getStringRestriction(String value) {
		if (value == null)
			value = "";
		return "%" + value + "%";
	}
}
